package cn.itcast.oa.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.List;
import java.util.zip.ZipInputStream;

import org.jbpm.api.ProcessDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.oa.base.BaseAction;

@Controller
@Scope("prototype")
public class ProcessDefinitionAction extends BaseAction{
     
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
   
	private String id;
	private String key;
	
	private File upLoad;
	private InputStream inputStream;
	
	public String list() throws Exception{
		List<ProcessDefinition> processDefinition= processDefinitionService.findAllLatestVersions();
    	ActionContext.getContext().put("processDefinition", processDefinition); 
		return "list";
      }
	
	public String delete() throws Exception{
		key=URLDecoder.decode(key,"utf-8");
		System.out.println(key);// 自己再进行一次URL解码
		processDefinitionService.deleteByKey(key);
  	  return "tolist";
    }

	public String addUI() throws Exception{
		return "addUI";
	}

	public String add() throws Exception {
		ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(upLoad));
		processDefinitionService.deploy(zipInputStream);
		zipInputStream.close();
		return "tolist";
	}

	public String downloadProcessImage() throws Exception{
		id=URLDecoder.decode(id, "utf-8");
		inputStream=processDefinitionService.getProcessImageResourceAsStream(id);
  	    return "downloadProcessImage";
    }
	//接收参数
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public File getUpLoad() {
		return upLoad;
	}

	public void setUpLoad(File upLoad) {
		this.upLoad = upLoad;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputSteam(InputStream inputStream) {
		this.inputStream = inputStream;
	}
}
